package com.hello.sandbox.fake.service;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import black.android.content.pm.BRIShortcutServiceStub;
import black.android.os.BRServiceManager;
import java.lang.reflect.Method;
import java.util.ArrayList;
import com.hello.sandbox.fake.hook.BinderInvocationStub;
import com.hello.sandbox.fake.hook.MethodHook;
import com.hello.sandbox.fake.hook.ProxyMethod;
import com.hello.sandbox.fake.service.base.PkgMethodProxy;
import com.hello.sandbox.utils.MethodParameterUtils;
import com.hello.sandbox.utils.compat.ParceledListSliceCompat;

/** Created by Milk on 4/5/21. * ∧＿∧ (`･ω･∥ 丶　つ０ しーＪ 此处无Bug 未实现，全部拦截 */
public class IShortcutManagerProxy extends BinderInvocationStub {

  public IShortcutManagerProxy() {
    super(BRServiceManager.get().getService(Context.SHORTCUT_SERVICE));
  }

  @Override
  protected Object getWho() {
    return BRIShortcutServiceStub.get()
        .asInterface(BRServiceManager.get().getService(Context.SHORTCUT_SERVICE));
  }

  @Override
  protected void inject(Object baseInvocation, Object proxyInvocation) {
    replaceSystemService(Context.SHORTCUT_SERVICE);
  }

  @Override
  public boolean isBadEnv() {
    return false;
  }

  @Override
  protected void onBindMethod() {
    super.onBindMethod();
    addMethodHook(new PkgMethodProxy("getShortcuts")); // 修复whtasApp启动黑屏问题
    addMethodHook(new PkgMethodProxy("disableShortcuts"));
    addMethodHook(new PkgMethodProxy("enableShortcuts"));
    addMethodHook(new PkgMethodProxy("getRemainingCallCount"));
    addMethodHook(new PkgMethodProxy("getRateLimitResetTime"));
    addMethodHook(new PkgMethodProxy("getIconMaxDimensions"));
    addMethodHook(new PkgMethodProxy("getMaxShortcutCountPerActivity"));
    addMethodHook(new PkgMethodProxy("reportShortcutUsed"));
    addMethodHook(new PkgMethodProxy("onApplicationActive"));
    addMethodHook(new PkgMethodProxy("hasShortcutHostPermission"));
    addMethodHook(new PkgMethodProxy("removeAllDynamicShortcuts"));
    addMethodHook(new PkgMethodProxy("removeDynamicShortcuts"));
    addMethodHook(new PkgMethodProxy("removeLongLivedShortcuts"));
    addMethodHook(
        new PkgMethodProxy("getManifestShortcuts") {
          @Override
          protected Object hook(Object who, Method method, Object[] args) throws Throwable {
            return ParceledListSliceCompat.create(new ArrayList<ShortcutInfo>());
          }
        });
  }

  @ProxyMethod("requestPinShortcut")
  public static class RequestPinShortcut extends MethodHook {
    @Override
    protected Object hook(Object who, Method method, Object[] args) throws Throwable {
      return true;
    }
  }

  @ProxyMethod("setDynamicShortcuts")
  public static class SetDynamicShortcuts extends MethodHook {
    @Override
    protected Object hook(Object who, Method method, Object[] args) throws Throwable {
      return true;
    }
  }

  @ProxyMethod("addDynamicShortcuts")
  public static class AddDynamicShortcuts extends MethodHook {
    @Override
    protected Object hook(Object who, Method method, Object[] args) throws Throwable {
      return true;
    }
  }

  @ProxyMethod("createShortcutResultIntent")
  public static class CreateShortcutResultIntent extends MethodHook {
    @Override
    protected Object hook(Object who, Method method, Object[] args) throws Throwable {
      return new Intent();
    }
  }

  @ProxyMethod("pushDynamicShortcut")
  public static class pushDynamicShortcut extends MethodHook {
    @Override
    protected Object hook(Object who, Method method, Object[] args) throws Throwable {
      return 0;
    }
  }

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    MethodParameterUtils.replaceAllAppPkg(args);
    return super.invoke(proxy, method, args);
  }
}
